package com.imedway.ihe.dtz.xds.registry.sub.camel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Association;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssociationType;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Folder;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.SubmissionSet;
import org.openehealth.ipf.commons.ihe.xds.core.requests.RegisterDocumentSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;

import com.imedway.ihe.dtz.xds.cons.ConstProperties;
import com.imedway.ihe.dtz.xds.dao.camel.FindBaseDao_CAMEL;
import com.imedway.ihe.dtz.xds.dao.camel.Iti42Dao_CAMEL;
import com.imedway.ihe.dtz.xds.entity.camel.XdsDocumentEntryPO;
import com.imedway.ihe.dtz.xds.util.DhccFileUtils;
import com.imedway.ihe.dtz.xds.vo.GetDocumentsVO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class Iti42SubRegistry_CAMEL {
	
	@Autowired
	private ConstProperties constProperties;
	
	@Autowired
	private Iti42Dao_CAMEL iti42Dao_CAMEL;
	
	@Autowired
	private FindBaseDao_CAMEL findBaseDao_CAMEL;
	
	@Transactional //小心的抓异常，不然影响事务
	public void run(EbXMLRegistryResponse30 ebXMLRegistryResponse30,RegisterDocumentSet registerDocumentSet) {
	 	
        log.info("----执行方法Iti42SubRegistry_CAMEL.run----");          
		Map<String, String> map = new HashMap<String, String>();//转换entryUuid   
		SubmissionSet submissionSet = registerDocumentSet.getSubmissionSet();
		if(submissionSet!=null) {
			iti42Dao_CAMEL.saveSubmissionSet(ebXMLRegistryResponse30, submissionSet,map);
		}	
		List<DocumentEntry> documentEntryList= registerDocumentSet.getDocumentEntries();
		if(documentEntryList!=null) {
			iti42Dao_CAMEL.saveDocumentEntryList(ebXMLRegistryResponse30, documentEntryList,map);
		}
		
		List<Folder> folderList = registerDocumentSet.getFolders();
		if(folderList!=null) {
			iti42Dao_CAMEL.saveFolderList(ebXMLRegistryResponse30, folderList,map);
//			createFoder(folderList);
		}
		
		List<Association> associationList = registerDocumentSet.getAssociations();
		if(associationList!=null) {
			iti42Dao_CAMEL.saveAssociationList(ebXMLRegistryResponse30, associationList,map);	
			handleAssociation(associationList,documentEntryList);
		}			
	}
	
	public void createFoder(List<Folder> folderList){
		try {	
			if(folderList!=null) {
				for(Folder folder:folderList) {
					if(folder.getTitle()!=null){
						DhccFileUtils.createDirectory(getFileRepositoryPath()+folder.getTitle().getValue());		
					}else {
						DhccFileUtils.createDirectory(getFileRepositoryPath()+folder.getEntryUuid());
					}
				}
			}
		}catch (Exception e) {
			log.info(e.getMessage(), e);
		}
	}
	
	public boolean handleAssociation(List<Association> associationList,List<DocumentEntry> documentEntryList){
		boolean flag = true;
		try {	
			if(associationList!=null) {
				for(Association association:associationList) {
					if(AssociationType.APPEND.getOpcode30().equals(association.getAssociationType())) {
						flag = APPENDFile(association,documentEntryList);
					}else if(AssociationType.REPLACE.getOpcode30().equals(association.getAssociationType())) {
						flag = REPLACEFile(association,documentEntryList);
					}else if(AssociationType.TRANSFORM.getOpcode30().equals(association.getAssociationType())) {
						flag = TRANSFORMFile(association,documentEntryList);
					}else if(AssociationType.TRANSFORM_AND_REPLACE.getOpcode30().equals(association.getAssociationType())) {
						flag = TRANSFORM_AND_REPLACEFile(association,documentEntryList);
					}							
				}
			}
		}catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return flag;
		
	}
	
	public String getFileRepositoryPath(){
		return constProperties.getFileRootPath()+"Repository\\";
		
	}
	
	public boolean APPENDFile(Association association,List<DocumentEntry> documentEntryList){
		boolean flag = true;
		DocumentEntry APPENDFile = new DocumentEntry();
		DocumentEntry targetFile = new DocumentEntry();
		String targetEntryUuid = null;
		for(DocumentEntry documentEntry:documentEntryList) {
			if(association.getSourceUuid().equals(documentEntry.getEntryUuid())) {
				APPENDFile=documentEntry;
				targetEntryUuid=association.getTargetUuid();
				break;
			}
		}
		GetDocumentsVO getDocumentsVO = new GetDocumentsVO();
		List<String> entryUuidList = new ArrayList<String>();
		entryUuidList.add(targetEntryUuid);
		getDocumentsVO.set$XDSDocumentEntryEntryUUID(entryUuidList);
		List<XdsDocumentEntryPO> xdsDocumentEntryPOList= findBaseDao_CAMEL.GET_XdsDocumentEntryPOs(getDocumentsVO);
		for(XdsDocumentEntryPO xdsDocumentEntryPO:xdsDocumentEntryPOList) {
			targetFile.setEntryUuid(xdsDocumentEntryPO.getEntryUuid());
			targetFile.setUri(xdsDocumentEntryPO.getUri());
		}
		
		try {
			DhccFileUtils.appendToFile(new FileInputStream(new File(APPENDFile.getUri())), new File(targetFile.getUri()));
		} catch (IOException e) {
			log.info(e.getMessage(), e);
		}
		
		return flag;
	}
	
	public boolean REPLACEFile(Association association,List<DocumentEntry> documentEntryList){
		boolean flag = true;
		
		return flag;
		
	}
	
	public boolean TRANSFORMFile(Association association,List<DocumentEntry> documentEntryList){
		boolean flag = true;
		
		return flag;
		
	}
	
	public boolean TRANSFORM_AND_REPLACEFile(Association association,List<DocumentEntry> documentEntryList){
		boolean flag = true;
		
		return flag;
		
	}

}

